<template>
	<view>
		<cu-custom :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{isEdit?'编辑话题':'创建话题'}}</block>
		</cu-custom>
		<view class="cu-card article">
			<view class="cu-item">
				<form>
					<view class="cu-form-group padding-sm">
						<view class="flex justify-between align-center" style="width: 100%;">
							<view class="flex justify-start align-center">
								<view class="label text-df padding-right">图标:</view>
								<view @tap="ViewImage" :data-url="avatar" class="cu-avatar round"
									:style="'background-image: url('+avatar+');'"></view>
								<view class="flex justify-start align-center">
									<view class="padding-left load-cuIcon"
										:class="requestStatus.isUpingAvatar?'loading':'over'"></view>
									<view v-if="requestStatus.isUpingAvatar">
										<text class="padding-left-xs text-grey text-sm">上传中</text>
									</view>
									<view v-else-if="requestStatus.isAvatarChanged">
										<text
											class="padding-left-xs text-grey text-sm">{{requestStatus.upingAvatarSuccess?'上传成功':'上传失败'}}</text>
									</view>
								</view>
							</view>
							<view @tap="ChooseAvatarImage"
								class="action lines-grey flex justify-center align-center solid">
								<text class="cuIcon-camera text-grey padding-sm"></text>
							</view>
						</view>
					</view>
					<view class="cu-form-group padding-sm">
						<view class="flex justify-between align-center" style="width: 100%;">
							<view class="flex justify-start align-center" style="width: 100%;">
								<view class="label text-df padding-right">标题:</view>
								<input style="font-size: 26upx;width: 100%;" class="text-sm" placeholder="请输入标题"
									v-model="title"></input>
							</view>
						</view>
					</view>
					<view class="cu-form-group align-start" style="height:300upx;">
						<view class="label text-df padding-right padding-top">介绍:</view>
						<textarea @input="contentUpdate" maxlength="-1" :value="content"
							placeholder="请输入话题介绍" style="height: 100%;"></textarea>
					</view>
				</form>
			</view>
		</view>
		<view class="flex justify-around align-center padding">
			<button @tap="commitAction()" :class="'cu-btn round '+themeColor" style="width: 65%;">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		uploadImage,
		createTopic,
		getTopicDetail
	} from '@/api/api.js'
	import {
		getUserInfo
	} from '@/api/auth.js'
	import config from '@/api/config.js'
	export default {
		data() {
			return {
				topic:null,
				themeColor:this.ThemeColor,
				appSetting:this.AppSetting,
				isEdit: false,
				content:null,
				title:null,
				avatar:null,
				requestStatus: {
					isAvatarChanged: false,
					isUpingAvatar: false,
					upingAvatarSuccess: false,
				}
			}
		},
		computed: {
			hasPaySubscribe() 
			{
				if(config.selfMallOpen == 0) {
					return false
				}
				return config.subscribeOpen == 1
			},
			uploadAvatarStatus() {
				if (!this.isAvatarChanged) {
					return ''
				}
				if (this.isUpingAvatar) {
					return '上传中'
				}
				if (this.upingAvatarSuccess) {
					return '上传成功'
				}
				return '上传失败'
			}
		},
		onLoad(options) {
			if (options.topicId !== null && options.topicId !== undefined) {
				this.isEdit = true
				this.topicId = options.topicId
				this.initTopicInfo()
			}
		},
		methods: {
			initTopicInfo() 
			{
				getTopicDetail(this.topicId).then(res=>{
					this.topic = res.data
					this.title = res.data.title
					this.content = res.data.introduce
					this.avatar = res.data.image
				}).catch(error=>{
					
				})
			},
			contentUpdate(e) {
				this.content = e.detail.value
			},
			createNewOne() {
				//找出已经修改的内容
				let params = {}
				if (this.title.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '没有标题'
					})
					return
				}
				params['title'] = this.title
				if(this.topic !== null) {
					if (this.avatar.length == 0) {
						uni.showToast({
							icon: 'none',
							title: '没有图标或者图标上传没成功'
						})
						return
					}
				}else{
					if (this.avatar.length == 0 || !this.requestStatus.upingAvatarSuccess) {
						uni.showToast({
							icon: 'none',
							title: '没有图标或者图标上传没成功'
						})
						return
					}
				}
				
				params['image'] = this.avatar
				if(this.content.length == 0) {
					uni.showToast({
						icon:'none',
						title:'请输入介绍'
					})
					return
				}
				if(this.content.length > 40) {
					uni.showToast({
						icon:'none',
						title:'话题介绍不可超过40个字'
					})
					return
				}
				params['introduce'] = this.content
				if(this.topic !== null) {
					params['topicId'] = this.topicId
				}
				uni.showLoading({
					title: '正在提交...'
				})
				createTopic(params).then(res => {
					uni.hideLoading()
					uni.navigateBack({
						delta: 1
					})
					let userInfo = getUserInfo() 
					if(userInfo.role_id == 0) {
						uni.$emit('ShowNetworkMsg','创建成功，请等待管理员审核通过~')						
					}else{
						uni.showToast({
							title: '创建成功'
						})
					}
				}).catch(error => {
					uni.hideLoading()
				})
			},
			commitAction() {
				this.createNewOne()
			},
			ChooseAvatarImage() {
				let this_ = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						let that_ = this_
						uni.navigateTo({
							url: '/pages/picture-cropper/picture-cropper?isIcon=1&url=' + res
								.tempFilePaths[0],
							events: {
								// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
								imageCropperFinish: function(data) {
									console.log(data.url)
									that_.avatar = data.url
									that_.requestStatus.isAvatarChanged = true
									let timestamp = new Date().getTime()
									let fileKey = String(timestamp)
									let _that = that_
									_that.requestStatus.isUpingAvatar = true
									uploadImage(data.url, fileKey, (imgUrl) => {
										console.log(imgUrl)
										_that.avatar = imgUrl
										_that.requestStatus.upingAvatarSuccess = true
										_that.requestStatus.isUpingAvatar = false
									}, (error) => {
										_that.requestStatus.upingAvatarSuccess = false
										_that.requestStatus.isUpingAvatar = false
									})
								}
							}
						})
					}
				});
			},
			ViewImage(e) {
				console.log(e.currentTarget.dataset.url)
				let currentUrl = e.currentTarget.dataset.url
				if(currentUrl == null || currentUrl.length == 0) {
					return
				}
				uni.previewImage({
					urls: [e.currentTarget.dataset.url],
					current: e.currentTarget.dataset.url
				});
			},
			toArr(object) {
				let arr = [];
				for (let i in object) {
					arr.push(object[i].text);
				}
				return arr;
			},
			//picker change切换事件
			columnPicker: function(e) {
				let value = e.detail.value;
				//如果两者下标不一致，表示滚动过
				if (this.value[0] !== value[0]) {
					this.areaArr = this.toArr(cityData);
					this.countryArr = this.toArr(cityData[value[0]].children);
					this.value = [value[0], 0]
				} else {
					this.value = value
				}
			},
			//确定按钮
			picker: function(e) {
				let value = this.value;
				if (cityData.length > 0) {
					let area = cityData[value[0]].text;
					let country = cityData[value[0]].children[value[1]].text;
					this.text = [area, country];
					this.showPickerStatus = false
				}
			},
			// 显示picker-view
			showPicker: function() {
				this.showPickerStatus = true
			},
			// 隐藏picker-view
			hidePicker: function() {
				this.showPickerStatus = false
			},
		}
	}
</script>

<style scoped>
	.label {
		color: #777777;
	}

	/* picker start*/

	.tui-mask-screen {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.6);
		z-index: 99996;
		transition: all 0.3s ease-in-out;
		opacity: 0;
		visibility: hidden;
	}

	.tui-mask-show {
		opacity: 1;
		visibility: visible;
	}

	.tui-picker-box {
		width: 100%;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 99999;
		visibility: hidden;
		transform: translate3d(0, 100%, 0);
		transform-origin: center;
		transition: all 0.3s ease-in-out;
		min-height: 20rpx;
		background: #fff;
	}

	.tui-pickerbox-show {
		transform: translate3d(0, 0, 0);
		visibility: visible;
	}

	.picker-header {
		width: 100%;
		height: 90rpx;
		padding: 0 46rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		font-size: 32rpx;
		background: #fff;
	}

	.tui-list-item::after {
		left: 0;
	}

	.btn-cancle {
		padding: 20rpx;
		color: #888;
	}

	.btn-sure {
		padding: 20rpx;
		color: #5677fc;
	}

	.picker-view {
		width: 100%;
		height: 260px;
	}

	.item {
		line-height: 50px;
		text-align: center;
	}

	/* picker end*/

	.search-box {
		width: 672rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
		margin: 80rpx auto 40rpx auto;
		position: relative;
		/* #ifdef MP-ALIPAY */
		background: #fff;
		/* #endif */
	}

	.search-box::after {
		content: '';
		position: absolute;
		height: 200%;
		width: 200%;
		border: 1rpx solid #dbe1ef;
		transform-origin: 0 0;
		-webkit-transform-origin: 0 0;
		-webkit-transform: scale(0.5);
		transform: scale(0.5);
		left: 0;
		top: 0;
		border-radius: 20rpx;
		pointer-events: none;
	}

	.s-img {
		width: 34rpx;
		height: 34rpx;
		margin-right: 17rpx;
		flex-shrink: 0;
	}

	.input {
		font-size: 32rpx;
		color: #353535;
		width: 565rpx;
		padding-right: 5rpx;
		box-sizing: border-box;
		height: 100%;
	}

	.img30 {
		height: 30rpx;
		width: 30rpx;
	}

	.text {
		color: #353535;
		font-size: 28rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.wid27 {
		width: 100%;
	}

	.wid46 {
		width: 100%;
	}

	.img32 {
		height: 32rpx;
		width: 32rpx;
	}

	.pdr20 {
		padding-right: 20rpx;
	}

	.flr {
		margin-left: 10rpx;
	}

	.btn-select {
		padding: 20rpx 40rpx;
	}
</style>
